package com.kltn.zigo.data.loader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;
import android.widget.ListView;

import com.kltn.zigo.data.IP;
import com.kltn.zigo.process.JSONParser;

public class CommentUserLoader {
	JSONParser jParser = new JSONParser(); 
    
	ListView list;
	
	private static String url_get_comment_user = IP.get_IP() +  IP.get_path_service() + "get_comment_user.php";
	
	 // JSON Node names
    public final String TAG_SUCCESS = "success";
    public final String TAG_COMMENT = "comment";
    public final String TAG_USER_ID = "user_id_zing";
    public final String TAG_USER_NAME = "user_name_zing";
    public final String TAG_COMMENT_ID = "comment_id";
    public final String TAG_COMMENT_TYPE = "type";
    public final String TAG_PLACE_ID = "place_id";
    public final String TAG_PLACE_NAME = "place_name";
    public final String TAG_PLACE_ADDRESS = "address";
    public final String TAG_PLACE_DISTRICT = "district_name";
    public final String TAG_PLACE_PROVINCE = "province_name";
    public final String TAG_URL_AVATAR = "url_avatar";
    public final String TAG_NAME_AVATAR = "name_avatar";
    public final String TAG_DETAIL_COMMENT = "detail_comment";
    public final String TAG_TIME_COMMENT = "time_comment";

    // products JSONArray
    JSONArray users = null;
    
    String user_id_zing;
    
    public CommentUserLoader(String UserIdZing)
    {
    	user_id_zing = UserIdZing;
    }
    
    public  ArrayList<HashMap<String, String>> GetCommentUser()
    {
    	ArrayList<HashMap<String, String>> commentusersList = new ArrayList<HashMap<String, String>>();
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("user_id_zing", user_id_zing));
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_get_comment_user, "POST", params);

        Log.d("All comment: ", json.toString());

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // products found
                // Getting Array of Products
            	users = json.getJSONArray(TAG_COMMENT);

                // looping through All Products
                for (int i = 0; i < users.length(); i++) {
                    JSONObject c = users.getJSONObject(i);

                    // Storing each json item in variable
                    String userId = user_id_zing;
                    String commentId = c.getString(TAG_COMMENT_ID);
                    String username = c.getString(TAG_USER_NAME);
                    String placeId = c.getString(TAG_PLACE_ID);
                    String placeName = c.getString(TAG_PLACE_NAME);
                    String placeAdress = c.getString(TAG_PLACE_ADDRESS);
                    String placeDistrict = c.getString(TAG_PLACE_DISTRICT);
                    String placeProvince = c.getString(TAG_PLACE_PROVINCE);
                    String detailComment = c.getString(TAG_DETAIL_COMMENT);
                    String timeComment = c.getString(TAG_TIME_COMMENT);
                    String typecomment = c.getString(TAG_COMMENT_TYPE);
                    String urlavatar = c.getString(TAG_URL_AVATAR);
                    String nameavatar = c.getString(TAG_NAME_AVATAR);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_USER_ID, userId);
                    map.put(TAG_USER_NAME, username);
                    map.put(TAG_COMMENT_ID, commentId);
                    map.put(TAG_PLACE_ID, placeId);
                    map.put(TAG_PLACE_NAME, placeName);
                    map.put(TAG_PLACE_ADDRESS, placeAdress);
                    map.put(TAG_PLACE_DISTRICT, placeDistrict);
                    map.put(TAG_PLACE_PROVINCE, placeProvince);
                    map.put(TAG_DETAIL_COMMENT, detailComment);
                    map.put(TAG_TIME_COMMENT, timeComment);
                    map.put(TAG_COMMENT_TYPE, typecomment);
                    map.put(TAG_URL_AVATAR, urlavatar);
                    map.put(TAG_NAME_AVATAR, nameavatar);

                    // adding HashList to ArrayList
                    commentusersList.add(map);
                }
            } 
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return commentusersList;
    }
}